package com.example.administrator.hqq.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.register.SetMessageActivity;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.bean.event.RegisterEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Method;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 康海涛
 * @describe:获取性别生日
 */
public class SetInformationActivity extends MyActivity {

    @BindView(R.id.radio_man)
    RadioButton radioMan;
    @BindView(R.id.radio_woman)
    RadioButton radioWoman;
    @BindView(R.id.loopView_year)
    LoopView loopViewYear;
    @BindView(R.id.loopView_month)
    LoopView loopViewMonth;
    @BindView(R.id.loopView_day)
    LoopView loopViewDay;
    @BindView(R.id.skip_activity)
    TextView skipActivity;
    @BindView(R.id.skip_activity_auto)
    RelativeLayout skipActivityAuto;
    private ArrayList<String> listYears;
    private ArrayList<String> listMonths;
    private ArrayList<String> listDays;
    private int choice_year = -1;
    private int choice_Month = -1;
    private String year = "1990年";
    private String month = "1月";
    private String day = "1日";
    private String sex;
    private String TAG = this.getClass().getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_information);
        ButterKnife.bind(this);
        initview();
        initdata();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initdata() {
        listYears = new ArrayList<String>();
        listMonths = new ArrayList<String>();
        listDays = new ArrayList<String>();
        for (int i = 1950; i < 2010; i++) {
            listYears.add(i + "年");
        }
        for (int i = 1; i <= 12; i++) {
            listMonths.add(i + "月");
        }
        //大月天数
        for (int i = 1; i <= 31; i++) {
            listDays.add(i + "日");
        }

        //年
        loopViewYear.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                choice_year = index+2;
                year = listYears.get(index);
                Log.e(TAG, "onItemSelected: 年" + year + "下标:" + choice_year);
                setDay();
            }
        });
        //设置原始数据
        loopViewYear.setItems(listYears);
        //月
        loopViewMonth.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                choice_Month = index;
                month = listMonths.get(index);
                Log.e(TAG, "onItemSelected: 年" + month + "下标:" + choice_Month);
                setDay();
            }
        });
        //设置原始数据
        loopViewMonth.setItems(listMonths);
        //日
        loopViewDay.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                day = listDays.get(index);
                Log.e(TAG, "onItemSelected: 日" + day);
            }
        });
        //设置原始数据
        loopViewDay.setItems(listDays);
        loopViewYear.setInitPosition(40);
        loopViewMonth.setInitPosition(0);
        loopViewDay.setInitPosition(0);
    }

    //根据选择的年份月份来确定天数
    private void setDay() {
        listDays.clear();
        if (choice_Month == 3 || choice_Month == 5 || choice_Month == 8 || choice_Month == 10) {
            //小月天数
            for (int i = 1; i <= 30; i++) {
                listDays.add(i + "日");
            }
        } else if (choice_Month == 1) {
            if (choice_year % 4 == 0) {

                //闰月天数
                for (int i = 1; i <= 29; i++) {
                    listDays.add(i + "日");
                }

            } else {
                //平月天数
                for (int i = 1; i <= 28; i++) {
                    listDays.add(i + "日");
                }
            }
        } else {
            //大月天数
            for (int i = 1; i <= 31; i++) {
                listDays.add(i + "日");
            }
        }
        loopViewDay.setItems(listDays);
    }

    private void initview() {
        //设置性别
        radioMan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                radioWoman.setChecked(false);
                sex = "1";
            }
        });
        radioWoman.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                radioMan.setChecked(false);
                sex = "0";
            }
        });
    }

    @OnClick(R.id.skip_activity_auto)
    public void onViewClicked() {
        Log.e(TAG, "onViewClicked: 生日:" + year + month + day + "性别:" + sex + "phone" + getIntent().getStringExtra(LoongsConfig.phone) + "code" + getIntent().getStringExtra(LoongsConfig.code));
        if (TextUtils.isEmpty(sex))
        {
            ToastUtils.showShort("请选择您的性别");
        }else{
            SetMessageActivity.start(this, getIntent().getStringExtra(LoongsConfig.phone), getIntent().getStringExtra(LoongsConfig.code), sex, year + month + day);
        }
    }

    public static boolean isHaveNavigationBar(Context context) {

        boolean isHave = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            boolean hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                isHave = false;
            } else if ("0".equals(navBarOverride)) {
                isHave = true;
            }
        } catch (Exception e) {
            Log.w("TAG", e);
        }
        return isHave;
    }

    public static void transportStatus(Activity context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

            if (!isHaveNavigationBar(context))

                context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }

    }
    protected int getContentViewId() {
        return R.layout.activity_set_information;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(RegisterEvent registerEvent){
        switch (registerEvent.getType()) {
            case BACK:
                finish();
                break;
        }
    }
    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }
}
